package com.hyf.ucenter.controller;


import com.hyf.commonutils.JwtUtils;
import com.hyf.commonutils.vo.OrederMember;
import com.hyf.commonutils.fruit;
import com.hyf.service.config.exception.GuliException;
import com.hyf.ucenter.entity.Member;
import com.hyf.ucenter.entity.Vo.LoginVo;
import com.hyf.ucenter.entity.Vo.RegisterVo;
import com.hyf.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2021-09-24
 */
@RestController
@RequestMapping("/ucenter/member")
//@CrossOrigin // 跨域
public class MemberController {


    @Autowired
    private MemberService memberService;

    // 登陆的方法
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public fruit loginUser(@RequestBody Member member){
          // member 封装手机号和密码
        // 调用service 里面的方法
        // 返回 token 值 ，使用 jwt 生成
        String token= memberService.login(member);

        return fruit.ok().data("token",token);

    }



    // 注册的方法
    @ApiOperation(value = "会员注册")
   @PostMapping("register")
    public fruit registerUser(@RequestBody RegisterVo registerVo) {

        // 调用方法
           memberService.register(registerVo);

           return fruit.ok();
    }





    // 根据token 获取用户信息
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getMemberInfo")
    public fruit getMemberInfo(HttpServletRequest request){
        // 调用 jwt 工具类的方法， 根据 request 对象获取头像，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("************"+memberId);
        // 调用数据库里面的方法，根据 用户id 获取用户信息
        Member member = memberService.getById(memberId);

        return fruit.ok().data("userInfo",member);

    }
    
    


    // 课件中的方法
    @ApiOperation(value = "根据token获取登录信息002")
    @GetMapping("auth/getLoginInfo")// 有bug
    public fruit getLoginInfo(HttpServletRequest request){

        try {

            String memberId = JwtUtils.getMemberIdByJwtToken(request);

            LoginVo loginInfoVo = memberService.getLoginInfo(memberId);

            return fruit.ok().data("item", loginInfoVo);

        }catch (Exception e){

            e.printStackTrace();

            throw new GuliException(20001,"error");

        }

    }
    
    
    
    
    
    //根据token字符串获取用户信息
    @GetMapping("getUcenter/{memberId}")
    public Member getUcenterById(@PathVariable("memberId") String memberId){
        Member member = memberService.getById(memberId);
        return member;
    }
    
    
    
    //根据token字符串获取用户信息02
    @PostMapping("getInfoUc/{id}")
    public Member getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        Member ucenterMember = memberService.getById(id);
        Member memeber = new Member();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
        
    }
    
    
    
    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public fruit countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return fruit.ok().data("countRegister",count);
    }
    
    // 根据用户id获取用户信息,用于远程调用
    @PostMapping("getUserInfoOrder/{id}")
    public OrederMember getUserInfoOrder(@PathVariable String id) {
        Member member=memberService.getById(id);
        // 把member对象里面的值复制给orderMember对象
        OrederMember orederMember=new OrederMember();
        BeanUtils.copyProperties(member, orederMember);
        
        
        return orederMember;
    }
    
    
    // 查询一天注册人数
    @GetMapping("countRegisterOrder/{day}")
    public fruit countRegisterOrder(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return fruit.ok().data("countRegister",count);
    }
    
    
}

